package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.pojo.po.*;
import com.newviews.tcmip.pojo.vo.EtcmCommonBaseInfoRespVO;
import com.newviews.tcmip.pojo.vo.export.EtcmPxNetworkExportReqVO;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmHerbServiceInf;
import com.newviews.tcmip.web.vo.combin.NetworkStatisticsRespVo;
import com.newviews.tcmip.web.vo.etcmherb.download.EtcmHerbNetworkExportReqVO;
import com.newviews.tcmip.web.vo.etcmherb.statistics.HerbStatisticsRespVo;
import com.newviews.tcmip.web.vo.etcmpx.PxIngrStatisticsRespVo;
import com.newviews.tcmip.web.vo.etcmpx.PxNetworkStatisticsRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中草药库 前端控制器
 * </p>
 *
 * @author lxc
 * @since 2024-02-29
 */
@RestController
@RequestMapping("/etcm/herb")
@Api(tags = "中草药库")
@Slf4j
public class EtcmHerbController {
    @Resource
    private EtcmHerbServiceInf etcmHerbServiceInf;
    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;

    @ApiOperation(value = "中草药库：字典-中草药分类")
    @GetMapping("/meridians/list")
    public ResultModel<List<EtcmHerbMeridiansPo>> getClsList(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode){
        return etcmHerbServiceInf.getMeridiansList(langCode);
    }

    @ApiOperation(value = "中草药库：字典-中草药五味")
    @GetMapping("/flavor/list")
    public ResultModel<List<EtcmHerbFlavorPo>> getFlavorList(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode){
        return etcmHerbServiceInf.getFlavorList(langCode);
    }

    @ApiOperation(value = "中草药库：字典-中草药四气")
    @GetMapping("/property/list")
    public ResultModel<List<EtcmHerbPropertyPo>> getPropertyList(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode){
        return etcmHerbServiceInf.getPropertyList(langCode);
    }


    @ApiOperation(value = "中草药库：查询")
    @GetMapping("/page")
    public ResultModel<PageModel<Map<String,String>>> getPage(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                              @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false) String content,
                                                              @RequestParam(required = false) String meridians,
                                                              @RequestParam(required = false) String flavor,
                                                              @RequestParam(required = false) String property,
                                                              @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPage(langCode,uiTypeCode,pageNo, pageSize, content,meridians,flavor,property,sortType);
    }


    @ApiOperation(value = "中草药库：统计")
    @GetMapping("/statistics")
    public ResultModel<HerbStatisticsRespVo> getStatistics(@RequestParam(required = false) String content){
        return etcmHerbServiceInf.getStatistics(content);
    }

    @ApiOperation(value = "中草药库：详情")
    @GetMapping("/details")
    public ResultModel<List<EtcmCommonBaseInfoRespVO>> getDetails(@RequestParam @NotBlank(message = "id不能为空") String herbId,
                                                                       @RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                       @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode){
        return etcmHerbServiceInf.getDetails(herbId,langCode,uiTypeCode);
    }





    @ApiOperation(value = "中草药构成: 相关成分查询")
    @GetMapping("/ingr/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByIngr(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                    @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                    @RequestParam @NotBlank(message = "中草药id不能为空") String herbId,
                                                                    @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                    @RequestParam(required = false) String content,
                                                                    @RequestParam(required = false) String ingrBbb,
                                                                    @RequestParam(required = false) String ingrCaco2,
                                                                    @RequestParam(required = false) String ingrHia,
                                                                    @RequestParam(required = false) String ingrHob,
                                                                    @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPageByIngr(langCode,uiTypeCode,herbId,pageNo, pageSize, content,ingrBbb,ingrCaco2,ingrHia,ingrHob,sortType);
    }

    @GetMapping("/statistics/ingredient")
    @ApiOperation("处方成分统计")
    public ResultModel<PxIngrStatisticsRespVo> getIngredientStatistics(@RequestParam(required = false) String content,
                                                                       @RequestParam String herbId){

        return etcmHerbServiceInf.getIngredientStatistics(content, herbId);
    }

    @ApiOperation(value = "中草药构成: 成分相关靶向查询")
    @GetMapping("/ingr/target/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByIngrTarget(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                          @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                          @RequestParam @NotBlank(message = "中草药id不能为空") String herbId,
                                                                          @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                          @RequestParam(required = false) String content,
                                                                          @RequestParam(required = false) String type,
                                                                          @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPageByIngrTarget(langCode,uiTypeCode,herbId,pageNo, pageSize, content,type,sortType);
    }

    @GetMapping("/target/statistics")
    @ApiOperation("获取成分靶向统计")
    public ResultModel<Map<String,String>> getTargetStatistics(@RequestParam(required = false) String content,
                                                               @RequestParam String herbId){

        return etcmHerbServiceInf.getTargetStatistics(content, herbId);
    }

    @ApiOperation(value = "中草药组学信息：查询")
    @GetMapping("/experimental/records/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByExperimentalRecords(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                                   @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                                   @RequestParam @NotBlank(message = "中草药id不能为空") String herbId,
                                                                                   @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                                   @RequestParam(required = false) String content,
                                                                                   @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPageByExperimentalRecords(langCode,uiTypeCode,herbId,pageNo, pageSize, content,sortType);
    }



    @ApiOperation(value = "中草药组合用药关系：结合中草药查询")
    @GetMapping("/combined/herbs/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByCombinedHerbs(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                             @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                             @RequestParam @NotBlank(message = "中草药id不能为空") String herbCode,
                                                                             @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                             @RequestParam(required = false, defaultValue = "100") Integer pageSize,
                                                                             @RequestParam(required = false) String content,
                                                                             @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPageByCombinedHerbs(langCode,uiTypeCode,herbCode,pageNo, pageSize, content,sortType);
    }


    @ApiOperation(value = "中草药组合用药关系：相关的疾病查询")
    @GetMapping("/associated/diseases/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByAssociatedDiseases(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                                  @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                                  @RequestParam @NotBlank(message = "中草药id不能为空") String herbCode,
                                                                                  @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                                  @RequestParam(required = false, defaultValue = "100") Integer pageSize,
                                                                                  @RequestParam(required = false) String content,
                                                                                  @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPageByAssociatedDiseases(langCode,uiTypeCode,herbCode,pageNo, pageSize, content,sortType);
    }





    @ApiOperation(value = "中草药：相关处方查询")
    @GetMapping("/px/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByPx(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                  @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                  @RequestParam @NotBlank(message = "中草药id不能为空") String herbId,
                                                                  @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                  @RequestParam(required = false) String content,
                                                                  @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbServiceInf.getPageByPx(langCode,uiTypeCode,herbId,pageNo, pageSize, content,sortType);
    }



    @GetMapping("/network/statistics")
    @ApiOperation("中草药network图")
    public ResultModel<NetworkStatisticsRespVo>networkStatistics(@RequestParam String herbId, @RequestParam String uiTypeCode, @RequestParam String langCode){
        return etcmHerbServiceInf.networkStatistics(herbId,langCode,uiTypeCode);
    }

    @GetMapping("/network/page")
    @ApiOperation("中草药network table")
    public ResultModel<PageModel<Map<String,String>>>networkPage(@RequestParam String herbId, @RequestParam String uiTypeCode, @RequestParam String langCode,@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        return etcmHerbServiceInf.networkPage(herbId,langCode,uiTypeCode,pageNo,pageSize);
    }
    @PostMapping("/network/download")
    @ApiOperation("中草药network图下载")
    public void networkDownload(@ApiIgnore HttpServletResponse response, @RequestBody EtcmHerbNetworkExportReqVO reqVO){
        try {
            ResultModel<PageModel<Map<String, String>>> res = etcmHerbServiceInf.networkPage( reqVO.getHerbId(),reqVO.getLangCode(),reqVO.getUiTypeCode(), 1,1000);
            if (!res.isSuccess()){
                log.error(res.getMessage());
                return;
            }

            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),res.getData().getRecords());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //    @ApiOperation(value = "中草药组合用药关系：相关文献查询")
//    @GetMapping("/treatise/page")
//    public ResultModel<PageModel<Map<String,String>>> getPageByTreatise(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
//                                                                        @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
//                                                                        @RequestParam @NotBlank(message = "中草药id不能为空") String herbId,
//                                                                        @RequestParam(required = false, defaultValue = "1") Integer pageNo,
//                                                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
//                                                                        @RequestParam(required = false) String content){
//        return etcmHerbServiceInf.getPageByTreatise(langCode,uiTypeCode,herbId,pageNo, pageSize, content);
//    }


}
